package com.suyuan.controller.admin;

import com.github.pagehelper.PageInfo;
import com.suyuan.entity.EmployeeInfo;
import com.suyuan.entity.Permission;
import com.suyuan.entity.PermissionEmployee;
import com.suyuan.service.PermissionEmployeeService;
import com.suyuan.utils.JsonMessage;
import com.suyuan.web.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 员工-权限控制类（角色控制类）
 */
@Controller
@RequestMapping(value = "/admin/permissionEmployee")
public class PermissionEmployeeContorller {
    @Resource(name = "permissionEmployeeService")
    private PermissionEmployeeService permissionEmployeeService;

    /**
     * 所有用户相关的权限
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(Integer empId, Integer perId, Page page , ModelMap model){
        PageInfo info = permissionEmployeeService.list(empId,perId,page);
        model.addAttribute("page",info);
        model.addAttribute("empId",empId);
        model.addAttribute("perId",perId);
        return "";
    }

    @RequestMapping(value = "/deleteByIds")
    @ResponseBody
    public Object deleteByIds(String ids){
        if(StringUtils.isBlank(ids)){
            return JsonMessage.error("删除失败");
        }
        String[] idArr = null;
        if(ids.length() >= 2){
            idArr = ids.split(",");
        }else{
            idArr = new String[]{ids};
        }
        for(String id : idArr){
            permissionEmployeeService.delete(Integer.parseInt(id));
        }
        return JsonMessage.success("删除成功");
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(Integer id){
        permissionEmployeeService.delete(id);
        return "";
    }

    /**
     * 跳转添加页面
     */
    @RequestMapping(value = "/add")
    public String add(ModelMap model){
        //员工全部id
        List<EmployeeInfo> employees = permissionEmployeeService.getEmployees();
        //权限全部id
        List<Permission> pers = permissionEmployeeService.getPermissions();
        model.addAttribute("employees",employees);
        model.addAttribute("pers",pers);
        return "";
    }

    @RequestMapping(value = "/save")
    public String save(PermissionEmployee pe){
        return "redirect:/admin/permissionEmployee/list";
    }

}
